package com.dupengcheng.subject.domain.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dupengcheng.subject.common.enums.IsDeleteEnum;
import com.dupengcheng.subject.domain.convert.SubjectCategoryConvert;
import com.dupengcheng.subject.domain.convert.SubjectLabelConvert;
import com.dupengcheng.subject.domain.entity.SubjectCategoryBO;
import com.dupengcheng.subject.domain.entity.SubjectLabelBO;
import com.dupengcheng.subject.domain.service.SubjectCategoryDomainService;
import com.dupengcheng.subject.infra.basic.entity.SubjectCategory;
import com.dupengcheng.subject.infra.basic.entity.SubjectLabel;
import com.dupengcheng.subject.infra.basic.service.SubjectCategoryService;
import com.dupengcheng.subject.infra.basic.service.SubjectLabelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

@Service
@Slf4j
public class SubjectCategoryDomainServiceImpl implements SubjectCategoryDomainService {

    @Resource
    private SubjectCategoryService subjectCategoryService;

    @Resource
    private SubjectLabelService labelService;


    @Override
    public void add(SubjectCategoryBO subjectCategoryBO) {
        if (log.isInfoEnabled()){
            log.info("SubjectCategoryController.add.bo:{}", JSONObject.toJSONString(subjectCategoryBO));

        }

        SubjectCategory subjectCategory = SubjectCategoryConvert.INSTANCE
                .convertBOToSubjectCategory(subjectCategoryBO);
        subjectCategory.setIsDeleted(IsDeleteEnum.UN_DELETE.getCode());
        subjectCategoryService.insert(subjectCategory);
    }

    @Override
    public List<SubjectCategoryBO> query(SubjectCategoryBO subjectCategoryBO) {
        //查询分类
        if (log.isInfoEnabled()){
            log.info("SubjectCategoryController.query.bo:{}", JSONObject.toJSONString(subjectCategoryBO));

        }
        SubjectCategory subjectCategory = SubjectCategoryConvert.INSTANCE
                .convertBOToSubjectCategory(subjectCategoryBO);
        subjectCategory.setIsDeleted(IsDeleteEnum.UN_DELETE.getCode());
       List<SubjectCategory> subjectCategoryList= subjectCategoryService.queryBatch(subjectCategory);
        //转为BO传出去。
        List<SubjectCategoryBO> subjectCategotyBoList=SubjectCategoryConvert.INSTANCE
                .convertCategoryToBOList(subjectCategoryList);

        return subjectCategotyBoList;

    }

    @Override
    public Boolean update(SubjectCategoryBO subjectCategoryBO) {
        //修改分类
        if (log.isInfoEnabled()){
            log.info("SubjectCategoryController.update.bo:{}", JSONObject.toJSONString(subjectCategoryBO));

        }
        //调用基础层、将BO转为普通实体类。

        SubjectCategory subjectCategory = SubjectCategoryConvert.INSTANCE
                .convertBOToSubjectCategory(subjectCategoryBO);
        subjectCategory.setIsDeleted(IsDeleteEnum.UN_DELETE.getCode());
        return subjectCategoryService.update(subjectCategory)>0;


    }

    @Override
    public List<SubjectCategoryBO> queryCategoryByPrimary(SubjectCategoryBO subjectCategoryBO) {

        //通过传来的父级ID，categoryId来查出子category和label

        SubjectCategory subjectCategory = subjectCategoryService.queryById(subjectCategoryBO.getId());
        Long parentId = subjectCategory.getParentId();
        if (new Long(0L).equals(parentId)){
            //如果是父级ID、则查询。
            SubjectCategory subjectCategoryEntity=new SubjectCategory();
            subjectCategoryEntity.setParentId(subjectCategory.getId());
            subjectCategoryEntity.setIsDeleted(IsDeleteEnum.UN_DELETE.getCode());
            List<SubjectCategory> child = subjectCategoryService.queryBatch(subjectCategoryEntity);
            if (child.size()==0){
                return Collections.emptyList();
            }
            //child是查出来的字分类。然后查它们的lable、放在BO里面进行返回。
            List<SubjectCategoryBO> subjectCategoryBOS = SubjectCategoryConvert.INSTANCE
                    .convertCategoryToBOList(child);
            for (SubjectCategoryBO category : subjectCategoryBOS) {

                SubjectLabel subjectLabel=new SubjectLabel();
                subjectLabel.setCategoryId(category.getId());
                List<SubjectLabel> subjectLabels = labelService.queryBatch(subjectLabel);
                if (subjectLabels.size()==0){
                    continue;
                }
                List<SubjectLabelBO> subjectLabelBOS = SubjectLabelConvert.INSTANCE
                        .convertLabelToBOList(subjectLabels);
                category.setLabelList(subjectLabelBOS);
            }
            return subjectCategoryBOS;
        }


        return Collections.emptyList();
    }
}
